package com.example.dispatherview;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Created by Alex-Zhang on 2018/1/8.
 */

public class MyDialog extends Dialog {

    private MyDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private final Context mContext;
        private boolean isCancelable = true;
        private boolean isCanTouchDismiss = true;
        private String title;
        private String content;
        private int themeId;
        private int layoutId;
        private OnClickListener positiveButtonListener, negativeButtonListener;
        private String negativeButtonText, positiveButtonText;
        private int animationId;
        public static final int CENTER_HORIZONTAL = Gravity.CENTER;
        public static final int BOTTOM_HORIZONTAL = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;

        @ORIENTATION
        private int Orientation = CENTER_HORIZONTAL;

        @IntDef({CENTER_HORIZONTAL, BOTTOM_HORIZONTAL})
        @Retention(RetentionPolicy.SOURCE)
        @interface ORIENTATION {
        }

        public Builder setOrientation(@ORIENTATION int orientation) {
            this.Orientation = orientation;
            return this;
        }

        public Builder(Context context) {
            this.mContext = context;
            this.themeId = R.style.Theme_AppCompat_Dialog;
            this.layoutId = R.layout.common_dialog;
        }


        public Builder setCanTouchDismiss(boolean canTouchDismiss) {
            this.isCanTouchDismiss = canTouchDismiss;
            return this;
        }

        public Builder setAnimation(int animationRes) {
            this.animationId = animationRes;
            return this;
        }


        public Builder setCancelable(boolean cancelable) {
            this.isCancelable = cancelable;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        public Builder setThemeId(int themeId) {
            this.themeId = themeId;
            return this;
        }

        public Builder setLayoutId(int layoutId) {
            this.layoutId = layoutId;
            return this;
        }

        public Builder setPositiveButtonListener(String positiveButtonText, OnClickListener positiveButtonListener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonListener = positiveButtonListener;
            return this;
        }

        public Builder setPositiveButtonListener(int positiveButtonTextId, OnClickListener positiveButtonListener) {
            this.positiveButtonText = mContext.getResources().getString(positiveButtonTextId);
            this.positiveButtonListener = positiveButtonListener;
            return this;
        }

        public Builder setNegativeButtonListener(String negativeButtonText, OnClickListener negativeButtonListener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonListener = negativeButtonListener;
            return this;
        }

        public Builder setNegativeButtonListener(int negativeButtonTextId, OnClickListener negativeButtonListener) {
            this.negativeButtonText = mContext.getResources().getString(negativeButtonTextId);
            this.negativeButtonListener = negativeButtonListener;
            return this;
        }


        public MyDialog build() {
            final MyDialog dialog = new MyDialog(mContext, themeId);
            Window window = dialog.getWindow();
            assert window != null;
            window.setContentView(layoutId);
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
            attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            window.setAttributes(attributes);
            window.setWindowAnimations(animationId);
            window.setGravity(Orientation);

            if (title != null) {
                ((TextView) window.findViewById(R.id.title_id)).setText(title);
            } else {
                window.findViewById(R.id.title_id).setVisibility(View.GONE);
            }

            if (content != null) {
                ((TextView) window.findViewById(R.id.content_id)).setText(content);
            } else {
                window.findViewById(R.id.content_id).setVisibility(View.GONE);
            }

            if (negativeButtonText != null) {
                ((Button) window.findViewById(R.id.negativeButton)).setText(negativeButtonText);
                if (negativeButtonListener != null) {
                    window.findViewById(R.id.negativeButton).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            negativeButtonListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                }
            } else {
                window.findViewById(R.id.negativeButton).setVisibility(View.GONE);
            }

            if (positiveButtonText != null) {
                ((Button) window.findViewById(R.id.positiveButton)).setText(positiveButtonText);
                if (positiveButtonListener != null) {
                    window.findViewById(R.id.positiveButton).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            positiveButtonListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                }
            } else {
                (window.findViewById(R.id.positiveButton)).setVisibility(View.GONE);
            }
            dialog.setCancelable(isCancelable);
            dialog.setCanceledOnTouchOutside(isCanTouchDismiss);
            return dialog;
        }
    }
}
